#include "ClearPlugin.h"
#include "Log.h"
#include "Expand/PluginsManager.h"
#include "Game/IInventory.hpp"
#include "Managers/GameManager.h"
#include "Components/ComponentInventory.h"
#include "Network/Client.h"

#include "GameSingleton.h"
#include "Network/PackageManager.h"
#include "Network/Packages/MessagePackage.h"
#include "Network/ServerRoot.h"
#include "Subsystems/SubsystemAnimalEntities.h"
#include "Subsystems/SubsystemPickables.h"

namespace PocketSurvival
{
    static const std::string titleText = "Clear";

    constexpr const char *help1 = "\n"
                                  "/clear animal    - 清理所有动物\n"
                                  "/clear item    - 清理所有掉落物\n";

    static const std::string normalHelp = help1;


    ClearPlugin::ClearPlugin()
    {
        m_animalEntities = GameSingleton::singletonPtr->getSingleton<SubsystemAnimalEntities>();
        m_pickables = GameSingleton::singletonPtr->getSingleton<SubsystemPickables>();
    }

    const std::string &ClearPlugin::getPluginName()
    {
        static const std::string pluginName = "清理插件";
        return pluginName;
    }

    const std::string &ClearPlugin::getIntroduce()
    {
        static const std::string introduce = "\n/clear animal - 清理所有动物";
        return introduce;
    }

    uint32_t ClearPlugin::getUseAuthLevel()
    {
        return 100;
    }

    AuthDisplayType ClearPlugin::getDisplayType()
    {
        return AuthDisplayType::Authority;
    }

    void ClearPlugin::processCmd(ChatObj &chatObj, std::vector<std::string> &cmdDataVec)
    {
        if (cmdDataVec.size() == 2)
        {
            if (cmdDataVec[1] == "animal")
            {
                m_animalEntities->animalLock.lock();

                const std::set<entt::entity> &entities = m_animalEntities->getAnimalEntitySet();
                uint32_t entitiesCount = entities.size();
                for (entt::entity entity : entities)
                {
                    m_animalEntities->removeAnimalEntity(entity);
                }

                m_animalEntities->animalLock.unlock();
                chatObj.sendMessage(titleText, fmt::format("成功清理动物，数量：{}", entitiesCount));
                return;
            }
            else if (cmdDataVec[1] == "item" || cmdDataVec[1] == "pickable")
            {
                std::vector<Pickable*> pickableVec;
                m_pickables->getNowPickables(pickableVec);
                for (Pickable *pickable : pickableVec)
                {
                    pickable->toRemove = true;
                }
                chatObj.sendMessage(titleText, fmt::format("成功清理凋落物，数量：{}", pickableVec.size()));
                return;
            }
        }
        chatObj.sendMessage(titleText, normalHelp);
    }

    void ClearPlugin::load() {}

    void ClearPlugin::getCmds(std::vector<std::string> &cmdVec)
    {
        cmdVec.push_back("/clear");
    }

} // namespace PocketSurvival


using namespace PocketSurvival;

extern "C" void initialize()
{
    GameSingleton::pluginsManager->registerPlugin<ClearPlugin>();
    Log::Info("成功加载 ClearPlugin 插件");
}